#include "./Include/CConfigIni.h"
#include <string>

std::string CConfigIni::APP_PATH = boost::filesystem::initial_path<boost::filesystem::path>().string();

std::shared_ptr<CConfigIni> CConfigIni::m_pInstance = nullptr;
CLock CConfigIni::m_lock;

CConfigIni::CConfigIni()
    :serverPort(SERVER_PORT)
    ,errMsg("")
{}

CConfigIni::~CConfigIni(){}

bool CConfigIni::LoadConfigIni(std::string ini_path)
{
    using namespace boost::property_tree;
    ptree lvptProperties;

    try
    {
        ini_parser::read_ini(ini_path, lvptProperties);             //读取配置文件

        //section:server
        {
            basic_ptree<std::string, std::string> lvbtItems = lvptProperties.get_child("server");                        
            serverPort = lvbtItems.get<unsigned int>("server_port");      
            maxCon = lvbtItems.get<unsigned int>("max_con");
        }                         
    }
    catch (std::exception& e)
    {
        errMsg = e.what();
        return false;
    }

	return true;
}

bool CConfigIni::SaveConfigIni(std::string ini_path)
{
    using namespace boost::property_tree;
    ptree lvptProperties;

    try
    {       
       lvptProperties.put<unsigned int>("server.server_port", serverPort);       
       ini_parser::write_ini(ini_path, lvptProperties);      
       ini_parser::write_ini(ini_path, lvptProperties); 
    }
    catch (std::exception& e)
    {
        errMsg = e.what();
        return false;
    }

    return true;
}